package com.atomone.epublib.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;

import com.atomone.epublib.model.EpubChapter;

import java.util.ArrayList;
import java.util.List;

import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Spine;
import nl.siegmann.epublib.domain.TOCReference;

/**
 * @author atomOne
 * @time 2018/6/10 下午1:43
 * @desc
 */
public class EpubUtils {

    /**
     * 获取封面
     *
     * @param input
     * @return
     */
    public static Bitmap resizeImage(byte[] input) {

        Bitmap bitmapOrg = BitmapFactory.decodeByteArray(input, 0, input.length);

        int height = bitmapOrg.getHeight();
        int width = bitmapOrg.getWidth();
        int newHeight = 256;

        float scaleHeight = ((float) newHeight) / height;

        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleHeight, scaleHeight);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);

        bitmapOrg.recycle();

        return resizedBitmap;
    }


    public static List<EpubChapter> getTableOfContents(Book book) {
        List<EpubChapter> epubChapters = new ArrayList<>();
        epubChapters.clear();
        if (book.getTableOfContents().getTocReferences().size() > 1) {
            ProcessChaptersByTOC(book.getTableOfContents().getTocReferences(), epubChapters, 3);
        } else {
            ProcessChaptersBySpline(book.getSpine(), epubChapters);
        }
        return epubChapters;
    }


    /**
     * 递归获取目录结构
     *
     * @param refs
     * @param epubChapters
     * @param level
     */
    private static void ProcessChaptersByTOC(List<TOCReference> refs, List<EpubChapter> epubChapters, int level) {

        if (refs == null || refs.isEmpty()) {
            return;
        }

        for (TOCReference TOC : refs) {

            String title = "";

            for (int i = 0; i < level; i++) {
                title += "  ";
            }

            title += TOC.getTitle();

            if (TOC.getResource() != null) {
                epubChapters.add(new EpubChapter(title, TOC.getCompleteHref()));
            }
            if (TOC.getChildren().size() > 0) {
                ProcessChaptersByTOC(TOC.getChildren(), epubChapters, level + 1);
            }
        }


    }

    /**
     * 获取目录结构
     *
     * @param spine
     * @param epubChapters
     */
    private static void ProcessChaptersBySpline(Spine spine, List<EpubChapter> epubChapters) {
        int ChapterNumber = 1;
        if (spine != null) {
            for (int i = 0; i < spine.size(); i++) {
                String title = spine.getResource(i).getTitle() != null ? spine.getResource(i).getTitle() : ChapterNumber + "";
                epubChapters.add(new EpubChapter(title, spine.getResource(i).getHref()));
                ChapterNumber++;
            }
        } else {
            Log.d("EpubReader", "spline is null");
        }
    }

}
